package common

import "github.com/chinahdkj/xorm"

//XormService XORM相关的服务
type XormService struct {
	Engine *xorm.Engine
}

//POST RESTful - create object
func (s XormService) POST(bean interface{}) error {
	_, err := s.Engine.Insert(bean)
	return err
}

//PUT RESTful - edit object
func (s XormService) PUT(id interface{}, bean interface{}, result interface{}) error {

	affected, err := s.Engine.Where("id=?", id).Update(bean)

	if affected > 0 {
		_, err := s.Engine.Id(id).Get(result)
		return err
	}

	return err
}

//DELETE RESTful - delete object
func (s XormService) DELETE(id interface{}, result interface{}) error {
	_, err := s.Engine.Where("id=?", id).Delete(result)
	return err
}

//GET RESTful - get object
func (s XormService) GET(id interface{}, result interface{}) error {

	_, err := s.Engine.Where("id=?", id).Get(result)
	return err
}
